#include "constants.h"

if (isMultiplayer) then {titleText ["", "BLACK FADED", 0.1]};

if (isNil "paramsArray") then {
	if (isClass (missionConfigFile/"Params")) then {
		for "_i" from 0 to (count (missionConfigFile/"Params") - 1) do {
			_paramName = configName ((missionConfigFile >> "Params") select _i);
			__mNsSetVar [_paramName, getNumber (missionConfigFile >> "Params" >> _paramName >> "default")];
			if (d_global_debug > 9) then { diag_log Format["ConfigFileParams%1: %2",_i, configName ((missionConfigFile >> "Params") select _i)];};
		};
	};
} else {
	for "_i" from 0 to (count paramsArray - 1) do {
		if (d_global_debug > 9) then { diag_log Format["ParamArray%1: %2",_i, paramsArray select _i];};
		__mNsSetVar [configName ((missionConfigFile >> "Params") select _i), paramsArray select _i];
	};
};

setViewDistance d_InitialViewDistance;

if (d_GrassAtStart) then {setterraingrid 25};

if (d_global_debug > 9) then { 
	// only for debugging, creates markers at all main target positions
	{
		_pos = _x select XF_MAP_LOCATION_CENTER;
		if (count _pos > 0) then {
			_name = _x select XF_MAP_LOCATION_UNIQUENAME;
			_size = _x select XF_MAP_LOCATION_SIZE;
			_marker= createMarkerLocal [_name, _pos];
			_marker setMarkerShapeLocal "ELLIPSE";
			_marker setMarkerColorLocal "ColorGreen";
			_marker setMarkerSizeLocal [_size,_size];
			_marker= createMarkerLocal [Format["%1_name",_name], _pos];
			_marker setMarkerTypeLocal "mil_dot";
			_marker setMarkerColorLocal "ColorBlack";
			_marker setMarkerSizeLocal [0.5,0.5];
			_marker setMarkerTextLocal _name;
			_marker setMarkerAlpha 0.5;
		};
	} forEach d_map_locations;
};

if (d_global_debug > 10) then { 
	// only for debugging, creates markers at all main target positions
	{
		_pos = _x select 0;
		_name = _x select 1;
		_size = _x select 2;
		_marker= createMarkerLocal [_name, _pos];
		_marker setMarkerShapeLocal "ELLIPSE";
		_name setMarkerColorLocal "ColorGreen";
		_name setMarkerSizeLocal [_size,_size];
		_name = _name + "xx";
		_marker= createMarkerLocal [_name, _pos];
		_marker setMarkerTypeLocal "mil_dot";
		_name setMarkerColorLocal "ColorBlack";
		_name setMarkerSizeLocal [0.5,0.5];
		_name setMarkerTextLocal _name;
		_marker setMarkerAlpha 0.5;
	} forEach d_town_markers;
};

if (d_global_debug > 10) then { 
	// only for debugging, creates markers at all main target positions
	{
		_pos = _x select 0;
		_name = _x select 1;
		_size = _x select 2;
		_marker= createMarkerLocal [_name, _pos];
		_marker setMarkerShapeLocal "ELLIPSE";
		_name setMarkerColorLocal "ColorBlue";
		_name setMarkerSizeLocal [_size,_size];
		_name = _name + "xx";
		_marker= createMarkerLocal [_name, _pos];
		_marker setMarkerTypeLocal "mil_dot";
		_name setMarkerColorLocal "ColorBlack";
		_name setMarkerSizeLocal [0.5,0.5];
		_name setMarkerTextLocal _name;
		_marker setMarkerAlpha 0.5;
	} forEach d_landmark_markers;
};

// This list can be used for random AI spawn like air KI and road KI.
d_all_markers = d_town_markers + d_landmark_markers;

if (isnil "d_all_ammoboxes") then { d_all_ammoboxes = []; };
_boxes = ["LocalBasicAmmunitionBox","LocalBasicWeaponsBox","GuerillaCacheBox","RUVehicleBox","RUOrdnanceBox","RULaunchersBox","RUSpecialWeaponsBox","RUBasicAmmunitionBox","RUBasicWeaponsBox","SpecialWeaponsBox","USVehicleBox","USOrdnanceBox","USLaunchersBox","USSpecialWeaponsBox","USBasicAmmunitionBox","USBasicWeaponsBox","Gunrack1","Gunrack2"];
if (count _boxes > 0) then {d_all_ammoboxes = [d_all_ammoboxes, _boxes] call XfarrayPushStack};

// When a box loaded into a vehicle is dropped. This will be the type of those boxes.
if (isnil "d_vehicle_drop_box") then {
	d_vehicle_drop_box = switch (d_own_side) do {
		case "GUER": {"RUOrdnanceBox"};
		case "EAST": {"RUOrdnanceBox"};
		case "WEST": {"RUOrdnanceBox"};
	};
};

// When a box is dropped by air support, this will be the type of the box.
if (isnil "d_air_drop_box") then {
	d_air_drop_box = switch (d_own_side) do {
		case "GUER": {"USOrdnanceBox"};
		case "EAST": {"USOrdnanceBox"};
		case "WEST": {"USOrdnanceBox"};
	};
};

// For other purposes or if the box type is not specified, it defaults to this box.
if (isnil "d_default_ammo_box") then {
	d_default_ammo_box = switch (d_own_side) do {
		case "GUER": {"LocalBasicAmmunitionBox"};
		case "EAST": {"RUBasicAmmunitionBox"};
		case "WEST": {"USBasicWeaponsBox"};
	};
};

// This is the type of the boxes spawned next to the flag at the base.
if (isnil "d_the_base_box") then {
	d_the_base_box  = switch (d_own_side) do {
		case "GUER": {"GuerillaCacheBox"};
		case "EAST": {"RUSpecialWeaponsBox"};
		case "WEST": {"USSpecialWeaponsBox"};
	};
};

d_side_enemy = switch (d_enemy_side) do {
	case "EAST": {east};
	case "WEST": {west};
	case "GUER": {resistance};
};

d_side_player = switch (d_own_side) do {
	case "EAST": {east};
	case "WEST": {west};
	case "GUER": {resistance};
};

d_own_side_char = switch (d_own_side) do {
	case "EAST": {"E"};
	case "WEST": {"W"};
	case "GUER": {"G"};
};

d_enemy_side_char = switch (d_enemy_side) do {
	case "EAST": {"E"};
	case "WEST": {"W"};
	case "GUER": {"G"};
};

d_side_player_str = switch (d_own_side) do {
	case "EAST": {"east"};
	case "WEST": {"west"};
	case "GUER": {"guerrila"};
};

d_own_side_trigger = d_own_side;

d_ai_enemy_sides = switch (d_own_side) do {
	case "EAST": {west};
	case "WEST": {east};
	case "GUER": {east};
};

d_rep_truck = ["MtvrRepair","KamazRepair","UralRepair_Base","V3S_Repair_TK_GUE_EP1"];
if (!isnil "d_veh_a_E") then {
	_repair = d_veh_a_E select XF_VEHICLE_TYPES_REPAIR;
	{
		if (!(_x in d_rep_truck)) then {
			d_rep_truck set [ count d_rep_truck, _x ];
		};
	}foreach _repair;
};
if (!isnil "d_veh_a_W") then {
	_repair = d_veh_a_W select XF_VEHICLE_TYPES_REPAIR;
	{
		if (!(_x in d_rep_truck)) then {
			d_rep_truck set [ count d_rep_truck, _x ];
		};
	}foreach _repair;
};
if (!isnil "d_veh_a_G") then {
	_repair = d_veh_a_G select XF_VEHICLE_TYPES_REPAIR;
	{
		if (!(_x in d_rep_truck)) then {
			d_rep_truck set [ count d_rep_truck, _x ];
		};
	}foreach _repair;
};


d_version_string = switch (d_own_side) do {
	case "EAST": {"east"};
	case "WEST": {"west"};
	case "GUER": {"guerrila"};
};

If (d_own_side == "WEST") then {
	FLAG_BASE setflagtexture "\ca\data\flag_usa_co.paa";
} else {
	FLAG_BASE setflagtexture "\ca\data\flag_rus_co.paa";
};

if (d_with_mgnest && isnil "d_mg_nest") then {
	d_mg_nest = (switch (d_own_side) do {
		case "GUER": {"GUE_WarfareBMGNest_PK"};
		case "EAST": {"RU_WarfareBMGNest_PK"};
		default {"USMC_WarfareBMGNest_M240"};
	});
};

if (d_with_aapod && isnil "d_aa_pod") then {
	d_aa_pod = (switch (d_own_side) do {
		case "GUER": {"Igla_AA_pod_East"};
		case "EAST": {"Igla_AA_pod_East"};
		default {"Stinger_Pod"};
	});
};

if (isnil "x_drop_array") then {
	x_drop_array = (switch (d_own_side) do {
			case "GUER": {[["Drop DSHKM", "Offroad_DSHKM_Gue","Mi17_Civilian"], ["Drop SPG9","Offroad_SPG9_Gue","Mi17_Civilian"], ["Drop Ammo", d_air_drop_box,"Mi17_Civilian"]]};
			case "WEST": {[["Drop HMMWV", "HMMWV_Armored","MV22"], ["Drop Avenger","HMMWV_Avenger","MV22"], ["Drop Ammo", d_air_drop_box,"MV22"]]};
			case "EAST": {[["Drop UAZ MG", "UAZ_MG_INS","Mi17_CDF"], ["Drop SPG9","UAZ_SPG9_INS","Mi17_CDF"], ["Drop Ammo", d_air_drop_box,"Mi17_CDF"]]};
		});
};

if (isnil "d_check_ammo_load_vecs") then {
	d_check_ammo_load_vecs = switch (d_own_side) do {
		case "WEST": {["M1126_ICV_M2_EP1","M1126_ICV_mk19_EP1","M1128_MGS_EP1","M1129_MC_EP1","M1130_CV_EP1","M1133_MEV_EP1","M1135_ATGMV_EP1","LandRover_Special_CZ_EP1","BAF_Jackal2_L2A1_D",
						"BAF_Jackal2_GMG_D","BAF_Jackal2_L2A1_W","BAF_Jackal2_GMG_W","LAV25_HQ","LAV25","MH60S","UH1Y","AH6J_EP1","AH6X_EP1","MH6J_EP1","CH_47F_EP1","UH60M_EP1","UH60M_MEV_EP1",
						"HMMWV_M1151_M2_CZ_DES_EP1", "HMMWV_M1151_M2_DES_EP1", "HMMWV_M998A2_SOV_DES_EP1","Mi171Sh_rockets_CZ_EP1", "Mi171Sh_CZ_EP1","MTVR_DES_EP1"]};
		case "EAST": {["V3S_TK_EP1","BTR40_MG_TK_INS_EP1","BTR40_TK_INS_EP1","BRDM2_TK_EP1","BTR60_TK_EP1",
						"BTR90","BTR90_HQ","Mi17_Ins","Mi17_rockets_RU","Mi17_TK_EP1","UH1H_TK_EP1","Mi24_D_TK_EP1","Mi17_TK_EP1"]};
		case "GUER": {["V3S_TK_GUE_EP1","Offroad_DSHKM_TK_GUE_EP1","Offroad_SPG9_TK_GUE_EP1","Pickup_PK_TK_GUE_EP1","V3S_Gue","Mi17_Ins","UH1H_TK_GUE_EP1"]};
	};
};
// side of the pilot that will fly the drop air vehicle
x_drop_side = d_own_side;

if (isNil "mg_nest_build_duration") then {mg_nest_build_duration = 3};
if (mg_nest_build_duration < 1) then {mg_nest_build_duration = 1};
if (isNil "mash_build_duration") then {mash_build_duration = 3};
if (mash_build_duration < 1) then {mash_build_duration = 1};
if (isNil "bagfence_build_duration") then {bagfence_build_duration = 3};
if (bagfence_build_duration < 1) then {bagfence_build_duration = 1};

d_objectID1 = objNull;
d_objectID2 = objNull;

_count_indestructible = 1;
while {!isnil format["INDESRUCTIBLE%1",_count_indestructible]} do {
	call compile Format ["%1 allowDamage false; %1 setVariable [""ARTY_N_LOG_IGNORE_OBJECT"",true];",format["INDESRUCTIBLE%1",_count_indestructible]];
	_count_indestructible = _count_indestructible + 1;
};
if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,"i_common.sqf","Indestructible Count",(_count_indestructible - 1)]; };

// The following make the base buidlings around the flag indestructible, so that it doesn't become a mess if the base get attacked.
if (!isNil "FLAG_BASE") then {
	_house_list= getPos FLAG_BASE nearObjects ["House",80];
	if (!isnil "_house_list") then {
		{
			if (d_global_debug > 9) then { diag_log Format["Made indestructible: %1", _x]; };
			_x allowDamage false;
			_x setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
		}foreach _house_list;
	};
	FLAG_BASE setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
};

// The following make the base buidlings around the respawn point indestructible, so that it doesn't become a mess if the base get attacked.
if (((getMarkerPos "respawn_west") distance [0,0,0]) > 0) then {
	_house_list= (getMarkerPos "respawn_west") nearObjects ["Building",40];
	if (!isnil "_house_list") then {
		{
			if (d_global_debug > 9) then { diag_log Format["Made indestructible: %1", _x]; };
			_x allowDamage false;
			_x setVariable ["ARTY_N_LOG_IGNORE_OBJECT",true];
		}foreach _house_list;
	};
};

// for markers and revive (same like NORRN_player_units)
// those are the name of the units ingame.
d_player_entities = ["RESCUE","RESCUE2","alpha_1","alpha_2","alpha_3","alpha_4","alpha_5","alpha_6","alpha_7","alpha_8","bravo_1","bravo_2","bravo_3","bravo_4","bravo_5","bravo_6","bravo_7","bravo_8","charlie_1","charlie_2","charlie_3","charlie_4","charlie_5","charlie_6","charlie_7","charlie_8","delta_1","delta_2","delta_3","delta_4","delta_5","delta_6","echo_1","echo_2","echo_3","echo_4","echo_5","echo_6","echo_7","echo_8","test_char"];
// Used for markers
d_player_roles = [];
if (!isDedicated) then {
	if (d_global_debug > 9) then { diag_log "Player roles loaded"; };
	d_player_roles = ["PLT LD","PLT SGT","SL","SN","MG","AT","GL","CM","AR","AM","TL","OP","GL","MG","MM","CM","GL","DS","SL","SN","AR","AT","RM","CM","MG","AT","EN","EN","EN","EN","EN","EN","SL","SN","AR","AT","RM","CM","MG","AT"];
};

if (d_global_debug > 9) then { 
	// For debugging only, create a marker at the position of the base
	_marker= createMarkerLocal ["BaseMarker", d_base_array select 0];
	_marker setMarkerShapeLocal "RECTANGLE";
	"BaseMarker" setMarkerColorLocal "ColorGreen";
	"BaseMarker" setMarkerSizeLocal [d_base_array select 1,d_base_array select 2];
	"BaseMarker" setMArkerDirLocal (d_base_array select 3);
};

// used to determine if we can call a new drop or not.
last_ammo_drop = -3423;
